package com.squirrel.michale;

import java.util.*;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/3/22 7:17 AM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode0833 {


    public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) {
        char[] chars = s.toCharArray();
        List<ReplaceElement> replaceElementList = new ArrayList<>();
        for (int i = 0; i < indices.length; i++) {
            replaceElementList.add(new ReplaceElement(indices[i], sources[i], targets[i]));
        }
        Collections.sort(replaceElementList, new Comparator<ReplaceElement>() {
            @Override
            public int compare(ReplaceElement o1, ReplaceElement o2) {
                return o2.index - o1.index;
            }
        });
        String result = s;
        for (int i = 0; i < replaceElementList.size(); i++) {
            ReplaceElement replaceElement = replaceElementList.get(i);
            int index = replaceElement.index;
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(result.substring(0, index));
            if (index + replaceElement.source.length() > s.length()) {
                continue;
            }
            String replace = new String(chars, index, replaceElement.source.length());
            if (replace.equals(replaceElement.source)) {
                String third = result.substring(index+replaceElement.source.length());
                stringBuffer.append(replaceElement.target);
                stringBuffer.append(third);
                result = stringBuffer.toString();
            }

        }
        return result;

    }

    public String findReplaceString2(String s, int[] indices, String[] sources, String[] targets) {


        List<ReplaceElement> replaceElementList = new ArrayList<>();


        for (int i = 0; i < indices.length; i++) {
            replaceElementList.add(new ReplaceElement(indices[i], sources[i], targets[i]));
        }

        Collections.sort(replaceElementList, new Comparator<ReplaceElement>() {
            @Override
            public int compare(ReplaceElement o1, ReplaceElement o2) {
                return o1.index - o2.index;
            }
        });


        String result = s;

        for (int i = indices.length-1; i >= 0; i--) {
            int index = indices[i];
            StringBuffer stringBuffer = new StringBuffer();
            if (index > 0) {
                stringBuffer.append(result.substring(0, index));
            }

            String second = result.substring(index);
            second = second.replace(sources[i], targets[i]);
            stringBuffer.append(second);
            result = stringBuffer.toString();
            System.out.println(result);

        }
        return result;

    }

    class ReplaceElement{
        int index;
        String source;
        String target;

        public ReplaceElement(int index, String source, String target) {
            this.index = index;
            this.source = source;
            this.target = target;
        }
    }


    public static void main(String[] args) {

//        输入：s = "abcd", indexes = [0,2], sources = ["a","cd"], targets = ["eee","ffff"]
//        输出："eeebffff"

        LeetCode0833 leetCode0833 = new LeetCode0833();
        String s = "abcd";
        int[] indexes = new int[] {0,2};
        String[] sources = new String[] {"a","cd"};
        String[] targets = new String[]{"eee", "ffff"};
        System.out.println(leetCode0833.findReplaceString(s, indexes, sources, targets));


        String s2 = "vmokgggqzp";
        int[] indices2 = new int[]{3, 5, 1};
        String[] sources2 = new String[]{"kg", "ggq", "mo"};
        String[] targets2 = new String[]{"s", "so", "bfr"};
//        "vmokgggqzp"
//        预期结果
//        "vbfrssozp"
        System.out.println(leetCode0833.findReplaceString(s2, indices2, sources2, targets2));


        String s3 = "ukdaxmobyfnelsdeecwisaxwjjfrxwsyfibbvnenesupbifdza";
        int[] indices3 = new int[]{22, 27, 0, 25, 6, 32, 30, 36, 44, 8, 46, 40, 38, 14, 17, 20};
        String[] sources3 = new String[]{"xwj", "rxw", "uk", "jf", "ob", "fib", "sy", "vn", "bi", "yfnel", "fdza", "esu", "en", "de", "cw", "sa"};
        String[] targets3 = new String[]{"pw", "lqpq", "vp", "w", "ey", "lavl", "o", "wvb", "kpj", "aosdke", "ybie", "kuh", "a", "b", "j", "gz"};

//        "vpdaxmeyaosdkesbejigzpwwlqpqolavlbwvbakuhpkpjykpje"
//        预期结果
//        "vpdaxmeyaosdkesbejigzpwwlqpqolavlbwvbakuhpkpjybie"

        System.out.println(leetCode0833.findReplaceString(s3, indices3, sources3, targets3));


        String s4 = "abcde";
        int[] indices4 = new int[]{2, 2};
        String[] sources4 = new String[]{"cdef", "bc"};
        String[] targets4 = new String[]{"f", "fe"};

        System.out.println(leetCode0833.findReplaceString(s4, indices4, sources4, targets4));

    }
}
